package com.luobu.javaee.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.luobu.javaee.email.MailCache;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface MailCacheMapper extends BaseMapper<MailCache> {

    @Insert("insert into mail_cache(email, code) values(#{email}, #{code})")
    void insert(String email, int code);

    // delete code 5 minutes after it is generated
    @Delete("delete from mail_cache where create_time < date_sub(now(), interval 5 minute)")
    void deleteExpiredCode();

    @Select("select code from mail_cache where email = #{email}")
    String selectCodeByEmail(String email);
}
